package com.example.whatrubbish.service.impl;

import com.example.whatrubbish.mapper.SearchRecordMapper;
import com.example.whatrubbish.model.SearchRecord;
import com.example.whatrubbish.service.SearchRecordService;
import com.example.whatrubbish.utils.GetIpAddrUtil;
import com.example.whatrubbish.vo.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class SearchRecordServiceImpl implements SearchRecordService {
    @Autowired
    SearchRecordMapper searchRecordMapper;

    @Override
    public int insertSelective(HttpServletRequest request, SearchRecord record) {
        String ip = GetIpAddrUtil.getIpAddr(request);
        record.setUserIp(ip);
        record.setSearchTime(new Date());
        record.setSearchStatus(0);
        return searchRecordMapper.insertSelective(record);
    }

    @Override
    public List selectSearchRecord(Criteria criteria) {
        return searchRecordMapper.selectSearchRecord(criteria);
    }

    @Override
    public Long selectSearchRecordCount(Criteria criteria) {
        return searchRecordMapper.selectSearchRecordCount(criteria);
    }

    @Override
    public List selectRecordWithPercent() {
        return searchRecordMapper.selectRecordWithPercent();
    }

    @Override
    public List<Map<String, Object>> selectFiveDaysRecordCount() {
        List list = searchRecordMapper.selectFiveDaysRecordCount();
        Collections.reverse(list);
        return list;
    }
}
